//
//  ViewController.m
//  CustromPlayer
//
//  Created by qianfeng01 on 15-4-24.
//  Copyright (c) 2015年 千锋. All rights reserved.
//

#import "ViewController.h"
#import "VideoView.h"
#import <AVFoundation/AVFoundation.h>
#define VideoURL @"http://hot.vrs.sohu.com/ipad1407291_4596271359934_4618512.m3u8" 
@interface ViewController ()
@property(nonatomic,strong)VideoView * videoView;
@property(nonatomic,strong) UISlider * slider;
@property(nonatomic,strong) AVPlayer * player;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createView];
}
- (void)createView{
    self.videoView = [[VideoView alloc] initWithFrame:CGRectMake(30, 60, 320, 320)];
    self.videoView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.videoView];
    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(40, 400, 80, 40);
    [button addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"播放" forState:UIControlStateNormal];
    [self.view addSubview:button];
    UIButton * button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(130, 400,80, 40);
    [button1 addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    [button1 setTitle:@"暂停" forState:UIControlStateNormal];

    [self.view addSubview:button1];
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(40, 450, 320, 40)];
    self.slider.minimumValue = 0.0;
    self.slider.maximumValue = 1.0;
    [self.slider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.slider];
}
- (void)valueChange:(UISlider *)slide{
    CGFloat progress = slide.value;
    CMTime timer = self.player.currentItem.duration;
    [self.player seekToTime:CMTimeMultiplyByFloat64(timer, progress)];
    
}

- (void)play:(UIButton *)button{
    if(self.player){
        [self.player play];
        return;
    }
    // 代表一个资源，代表一个音频或视频

    // 网络的
//    AVURLAsset * asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:VideoURL] options:nil];
    
    // 本地的
    NSString * path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp4"];
    NSLog(@"path %@",path);
    NSURL * utl = [NSURL fileURLWithPath:path];
    AVURLAsset * asset = [[AVURLAsset alloc] initWithURL:utl options:nil];
    // 对资源的状态描述
    
    // AVPlayerItem 会对视频进行预加载，预加载完成之后会对状态信息进行跟新，我们只有监听合适的状态信息就可以了,对资源进行描述
    AVPlayerItem * playerItem = [AVPlayerItem playerItemWithAsset:asset];
    self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
    [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
    // 定期调用
    // CMTime 才、指定影片播放的时间 value/timeScale 可以换算成秒
    __weak typeof(self) mysel = self;
    [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
     CMTime totalTime =    mysel.player.currentItem.duration;
     CGFloat totalsecon =   CMTimeGetSeconds(totalTime);
     CGFloat currentSecond =CMTimeGetSeconds(time);
     mysel.slider.value = currentSecond/totalsecon;
    }];
    // 和视图进行关联
    [self.videoView setPlayer:self.player];
    //[self.player play];
    
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"status"]) {
        AVPlayerItem * playerItem =  ( AVPlayerItem *)object;
        NSLog(@"status %ld",playerItem.status);
        if (playerItem.status==AVPlayerItemStatusReadyToPlay) {
            [self.player play];
        }else{
            NSLog(@"有问题");
        }
    }
}
- (void)pause:(UIButton *)button{
    if(self.player)
    [self.player pause];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
